package com.example.shopping2.a_c_fragment.car

import BaseActivity
import IActivity
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.appcompat.widget.AppCompatButton
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.shopping2.R
import com.example.shopping2.a_a_entity.BaseEntity
import com.example.shopping2.a_a_mvp.contract.HomeContract
import com.example.shopping2.a_a_mvp.model.models.HomeModel
import com.example.shopping2.a_a_mvp.presenter.presenters.HomePresenter
import com.example.shopping2.a_c_fragment.car.adapter.Address_Adapter
import com.example.shopping2.a_c_fragment.car.entity.OrderGoodsEntity
import com.example.shopping2.a_c_fragment.car.entity.ShipAddressEntity
import com.example.shopping2.a_c_network.HttpCode
import com.example.shopping2.a_c_network.LjlUtils

class AddressListActivity : BaseActivity<HomePresenter>(),HomeContract.HomeView,IActivity {
    private val addressBack: ImageView by lazy { findViewById<ImageView>(R.id.address_back) }
    private val mAddAddressBtn: AppCompatButton by lazy { findViewById<AppCompatButton>(R.id.mAddAddressBtn) }
    private val mAddressRv: RecyclerView by lazy { findViewById<RecyclerView>(R.id.mAddressRv) }
    private var userid: Long? = 0
    private var adapter  = Address_Adapter()


    override fun findlayout(): Int {
        return R.layout.activity_address_list
    }

    override fun initView() {
        userid =
            getSharedPreferences("loging", AppCompatActivity.MODE_PRIVATE)
                ?.getLong("id", 1)
        addressBack.setOnClickListener {
            finish()
        }
        mAddAddressBtn.setOnClickListener {
                startActivity(Intent(this,Add_Address_Activity::class.java))
        }
    }

    override fun initData() {
       request()

    }

    override fun onRestart() {
        super.onRestart()
        request()
    }
    //发送广播来通知那边获取地址
    override fun finish() {
        super.finish()

    }

    fun request(){
        mypre = HomePresenter(HomeModel(),this)
        //获取订单
        var list: ArrayList<HashMap<String, Any>> = ArrayList()
        var map: HashMap<String, Any> = HashMap()
        map.put("code",HttpCode.ADDRESSLIST)
        map.put("id",userid!!)
        list.add(map)
        mypre.requestAll(list)
    }

    override fun showHomeData(baseEntity: BaseEntity) {

        if (baseEntity is ShipAddressEntity){
           var data = baseEntity.data

            LjlUtils.Ljl_Loge("===============")

            if (data == null){

            }else{
                adapter.setNewInstance(data)
                mAddressRv.adapter = adapter
                mAddressRv.layoutManager = LinearLayoutManager(this)
                mAddressRv.addItemDecoration(DividerItemDecoration(this,DividerItemDecoration.VERTICAL))

            }

        }else if(baseEntity.status.equals("0")){
            LjlUtils.Ljl_Loge("----------------")
//            LjlUtils.Ljl_Toast(this,""+baseEntity.message)

            request()

        }

    }

}